package com.xiong.wuliao.util;

import com.xiong.wuliao.entity.HomeStorys;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 解析Home页面服务器传过来的数据
 * Created by Administrator on 2016/11/4.
 */
public class HomeJSON {
    // 如果lists在方法外面new 就只适合一个页面使用，要几个页面都使用就需要在下面new
    public static List<HomeStorys> saveHomeStory(String s) {
        List<HomeStorys> lists = new ArrayList<>();
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(s.trim());
            JSONObject object = jsonObject.optJSONObject("data");

            JSONArray array = object.optJSONArray("items");
            for (int index = 0; index < array.length(); index++) {
                HomeStorys homeStory = new HomeStorys();
                JSONObject obj = array.optJSONObject(index);

                homeStory.setContent_url(obj.getString("content_url"));
                homeStory.setCover_image_url(obj.getString("cover_image_url"));
                homeStory.setCreated_at(obj.getString("created_at"));
                homeStory.setId(obj.getString("id"));
                homeStory.setLiked(obj.getBoolean("liked"));
                homeStory.setLikes_count(obj.getString("likes_count"));
                homeStory.setPublished_at(obj.getString("published_at"));
                homeStory.setShare_msg(obj.getString("share_msg"));
                homeStory.setShort_title(obj.getString("short_title"));
                homeStory.setStatus(obj.getString("status"));
                homeStory.setTitle(obj.getString("title"));
                homeStory.setType(obj.getString("type"));
                homeStory.setUpdated_at(obj.getString("updated_at"));
                homeStory.setUrl(obj.getString("url"));

                lists.add(homeStory);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return lists;
    }

}
